package com.io.basic;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;



/**
 * @author Lenovo
 *
 */
public class FileWriterWorker implements Runnable   {

	private final FileOutputStream ouputSource;
	private final String fixedString;

	public FileWriterWorker(FileOutputStream fileOs)
	{
		this.ouputSource =  fileOs;
		fixedString = "Abhishek Choudhury";
	}
	
	public FileWriterWorker(FileOutputStream fileOs,String fixedStr)
	{
		this.ouputSource =  fileOs;
		this.fixedString=fixedStr;
		System.err.println("Cons called with fixed Str"+fixedStr);
		
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		writeFileWithInterrupt(ouputSource);
		
	}

	private void writeFileWithInterrupt(FileOutputStream ouputSource) {
//		BufferedWriter br = new BufferedWriter(new OutputStreamWriter(ouputSource));
		int lineCounter = 0;
		while(lineCounter<100)
		{
//			System.out.println(fixedString);
			try {
				ouputSource.write((lineCounter++ +" :"+fixedString+"\r").getBytes());
				ouputSource.flush();
				Thread.sleep(300);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		try {
			
			ouputSource.close();
			//br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		
	}

}
